package com.zwd.deep.controller;


import com.zwd.deep.common.RequestResult;
import com.zwd.deep.entity.dto.DoctorsLoginDto;
import com.zwd.deep.entity.dto.DoctorsPageDTO;
import com.zwd.deep.entity.vo.DoctorsVo;
import com.zwd.deep.service.IDoctorsService;
import com.zwd.deep.utils.HttpServletUtil;
import com.zwd.deep.utils.JwtUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 医生信息表
 *
 * @author zwd
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/doctors")
@AllArgsConstructor
@Slf4j
@Validated
public class DoctorsController {
    private final IDoctorsService doctorsService;

    /**
     * 登录
     * @param doctorsLoginDto 医生登录参数
     * @return token
     */
    @PostMapping("/login")
    public RequestResult<String> login(@Validated @RequestBody DoctorsLoginDto doctorsLoginDto) {
        log.info("login userLoginDto: {}", doctorsLoginDto);
        String token = doctorsService.login(doctorsLoginDto);
        return RequestResult.success(token);
    }

    /**
     * 根据医生id获取医生信息
     * @return 医生信息
     */
    @GetMapping("getByDoctorId")
    public RequestResult<DoctorsVo> getByDoctorId(Long doctorId) {
        if(Objects.isNull(doctorId)) {
            doctorId = Long.valueOf(JwtUtil.extractId(HttpServletUtil.getToken()));
        }
        DoctorsVo doctorsVo = doctorsService.getByDoctorId(doctorId);
        return RequestResult.success(doctorsVo);
    }


    /**
     * 分页查询
     * @param doctorsPageDTO 分页参数
     * @return 分页结果
     */
    @PostMapping("/page")
    public RequestResult<List<DoctorsVo>> page(@RequestBody DoctorsPageDTO doctorsPageDTO) {
        List<DoctorsVo> doctorsVos = doctorsService.getPage(doctorsPageDTO);
        return RequestResult.success(doctorsVos);
    }
}
